package class01;

/**
 * @author Gong Cheng
 * @date 2021/2/25 1:49 下午
 **/
public class Code07_EvenTimesOddTimes {

    // arr中, 只有一种数，出现奇数层次
    public static void priintOddTimesNum1(int[] arr){
        int eor = 0;
        for(int i = 0; i < arr.length; i++){
            eor ^= arr[i];
        }
        System.out.println(eor);
    }

    // arr中，有两种数，出现奇数次
    public static void printOddTimesNum2(int[] arr){
        int eor = 0;
        for (int i = 0; i < arr.length; i++){
            eor ^= arr[i];
        }
        // 提取最右侧的1
        int rightOne = eor & (~eor + 1);
        int onlyOne = 0;
        for (int i = 0; i < arr.length; i++){
            if((arr[i] & rightOne) != 0){
                onlyOne ^= arr[i];
            }
        }
        System.out.println(onlyOne + " "+ (eor ^ onlyOne));
    }

    // 计算一个数字中1的个数
    public static int bit1counts(int N){
        int count = 0;
        while (N != 0){
            int rightOne = N & ((~N) + 1);
            count ++;
            N ^= rightOne;
        }
        return count;
    }

    public static void main(String[] args) {
        printOddTimesNum2(new int[]{1,1,2,2,3,3,4,4,5,6});
    }
}
